package com.tistory.commons.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;

public class DbUtil {
	private static final Logger logger = Logger.getLogger(DbUtil.class.getName());
    /**
     * CLOB 占쏙옙체占쏙옙 占쏙옙占승댐옙.
     * @param prop 占쌔댐옙 占쏙옙체占쏙옙
     * @return
     */
    public static Object getClob(Object prop){
        if(prop ==null)
            return prop;
        if(prop instanceof String)
            return prop;

        BufferedReader reader = null;
        StringBuffer buff = new StringBuffer();
       
        try {
            if (prop instanceof oracle.sql.CLOB){
                reader = new BufferedReader(((oracle.sql.CLOB)prop).getCharacterStream());
            }
            for(String line = null; (line =reader.readLine())!=null;){
                buff.append(line+"\r\n");
            }

        } catch (Exception e) {

            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally{
            try {
                if(reader!=null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                reader=null;
            }
        }

        return buff.toString();
    }

    /**
     * CLOB 占쏙옙체占쏙옙 占쏙옙占쏙옙磯占?
     * @param obj 占쌔댐옙 占쏙옙체占쏙옙
     * @param data 占쏙옙占쏙옙占?占쏙옙占쏙옙占쏙옙
     */
    public static void setClob(Object obj,Object data){
        BufferedWriter writer = null;
        try {

            if(obj instanceof oracle.sql.CLOB)
                writer = new BufferedWriter(((oracle.sql.CLOB)obj).getCharacterOutputStream());
            writer.write((String)data);

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally{
            try {
                if(writer!=null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                writer=null;
            }
        }
    }

    /**
     * BLOB 占쏙옙체占쏙옙 占쏙옙占승댐옙.
     * @param prop 占쌔댐옙 占쏙옙체占쏙옙
     * @return
     */
    public static byte[] getBlob(Object prop){
        if(prop ==null)
            return null;

        logger.debug("DbUtil.getBlob> "+prop.getClass().getName());

        BufferedInputStream reader = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[]  returnByte = null;
        try {
            if (prop instanceof oracle.sql.BLOB){
                reader = new BufferedInputStream(((oracle.sql.BLOB)prop).getBinaryStream());

            }
            Date startTime = new Date(System.currentTimeMillis());            
            logger.debug("占?占신?DB占쏙옙 Blob占쏙옙占쏙옙 占쏙옙  占시곤옙 :"+startTime);
            for(byte[] readOut = new byte[4096]; (reader.read(readOut))>=0;){
                out.write(readOut);
            }
            Date endTime = new Date(System.currentTimeMillis());
            logger.debug("占?占신?DB占쏙옙 Blob占쏙옙占쏙옙 占쏙옙  占시곤옙 :"+endTime);

            returnByte = out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally{
            try {
                if(reader!=null)
                    reader.close();
                if(out!=null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                reader=null;
                out = null;
            }
        }

        return returnByte;
    }

    /**
     * BLOB 占쏙옙체占쏙옙 占쏙옙占쏙옙磯占?
     * @param obj 占쌔댐옙 占쏙옙체 占쏙옙
     * @param data 占쏙옙占쏙옙占?占쏙옙占쏙옙占쏙옙 占썼열
     */
    public static void setBlob(Object obj,byte[] data){
        BufferedOutputStream writer = null;

        logger.debug("DbUtil.setBlob> "+obj.getClass().getName());

        try {

            if(obj instanceof oracle.sql.BLOB)
                writer = new BufferedOutputStream(((oracle.sql.BLOB)obj).getBinaryOutputStream(), 4096);
            
            logger.debug("Buffer Size:4096");
            Date startTime = new Date(System.currentTimeMillis());            
            logger.debug("占?占신?DB占쏙옙 Blob占쏙옙占쏙옙 占쏙옙  占시곤옙 :"+startTime);
            
            writer.write(data);
            
            Date endTime = new Date(System.currentTimeMillis());
            logger.debug("占?占신?DB占쏙옙 Blob占쏙옙占쏙옙 占쏙옙  占시곤옙 :"+endTime);
            
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally{
            try {
                if(writer!=null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                writer=null;
            }
        }
    }

}
